package com.teamclub.erp.sservices

import java.text.SimpleDateFormat
import java.util.Date

import io.ebean.Ebean
import com.fasterxml.jackson.databind.JsonNode
import com.teamclub.app.sforms.AppSubjectUserForm
import com.teamclub.app.sservices.LoginService
import com.teamclub.domain.system.{AppDomainInstance, AppSubjectUser}
import com.teamclub.erp.sforms.LoginForm
import com.teamclub.util.errors.ErrorCode
import com.teamclub.util.libs.{Eithers, F}
import org.apache.commons.lang3.StringUtils
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class ErpLoginService {
  @Autowired
  var loginService: LoginService = _

  val logger = LoggerFactory.getLogger(getClass)
  def modifyPwd(form: LoginForm): F.Either[Boolean, ErrorCode] = {
    if(StringUtils.isBlank(form.telephone) || StringUtils.isBlank(form.password) || StringUtils.isBlank(form.userName)) {
      return F.Either.Right(ErrorCode.BIZ_FAILURE)
    }
    var userName = ""
    var organNo = ""
    if(form.userName.contains("@")) {
      userName = form.userName.substring(0, form.userName.indexOf("@"))
      organNo = form.userName.substring(form.userName.indexOf("@"))
    } else {
      userName = form.userName
      organNo = form.userName
    }
    val instance = Ebean.find(classOf[AppSubjectUser]).where()
        .eq("appDomain", "WINDER-DEALER")
        .eq("telephone", form.telephone)
        .eq("userName", userName)
        .eq("organNo", organNo)
        .findOne()
    if(instance == null) {
      return F.Either.Right(ErrorCode.ENTITY_NOT_FOUND)
    }
    instance.setUserPassword(form.password)
    instance.update()
    F.Either.Left(true)
  }

  def login(form: AppSubjectUserForm): F.Either[JsonNode, ErrorCode] = {
    var userName = ""
    var organNo = ""
    if(form.userName.indexOf("@") == -1) {
      userName = form.userName
      organNo = form.userName
    } else {
      userName = form.userName.substring(0, form.userName.indexOf("@"))
      organNo = form.userName.substring(form.userName.indexOf("@") + 1)
    }
    val appDomainInstance = Ebean.find(classOf[AppDomainInstance]).where().eq("organNo", organNo).findOne()
    if(appDomainInstance == null) {
      return F.Either.Right(ErrorCode.ref("未查询到该用户"))
    }
    if(appDomainInstance.getAppDomain != "WINDER-DEALER" && appDomainInstance.getAppDomain != "WINDER-PRODUCER") {
      return F.Either.Right(ErrorCode.ref("所属域错误"))
    }
    try {
      if(appDomainInstance.getValidateDate == null) {
        return F.Either.Right(ErrorCode.ref("该账户无使用期限"))
      }
      val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
      val s = dateFormat.format(appDomainInstance.getValidateDate)
      val date = dateFormat.parse(s)
      if(date.before(new Date)){
        return F.Either.Right(ErrorCode.ref("该实体已过期"))
      }
      return loginService.exits(form)
    } catch {
      case e: Exception => {
        logger.error("", e)
        return F.Either.Right(ErrorCode.ref("有效期解析失败"))
      }
    }

  }

}
